package com.example.back.impl;

import com.example.back.mapper.UserMapper;
import com.example.back.model.User;
import com.example.back.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> findAllUser() {
        return userMapper.findAllUser();
    }

    @Override
    public List<User> findByInformation(String information) {
        return userMapper.findByInformation(information);
    }

    @Override
    public User findByAccount(String account) {
        return userMapper.findByAccount(account);
    }

    @Override
    public User findByPhone(String phone) {
        return userMapper.findByPhone(phone);
    }

    //更新用户信息
    @Override
    public Boolean updateUsername(String account, String username) {
        return userMapper.updateUsername(account, username);
    }
    @Override
    public Boolean updateSex(String account, int sex) {
        return userMapper.updateSex(account, sex);
    }
    @Override
    public Boolean updatePhone(String account, String phone) {
        return userMapper.updatePhone(account, phone);
    }
    @Override
    public Boolean updateEmail(String account, String email) {
        return userMapper.updateEmail(account, email);
    }
    @Override
    public Boolean updatePassword(String account, String password) {
        return userMapper.updatePassword(account, password);
    }


    @Override
    public void addUser(String account, String username, String password, String phone) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        if(userMapper.addUser(account, username, password, phone, time)) {
        }
    }

    @Override
    public List<User> userSearch(String userInfo) {
        List<User> userList = null;
        if (userMapper.findByUsername(userInfo) != null) {
            userList = userMapper.findByUsername(userInfo);
        }
        if (userMapper.findByAccount(userInfo) != null) {
            userList.add(userMapper.findByAccount(userInfo));
        }
        if (userMapper.findByPhone(userInfo) != null) {
            userList.add(userMapper.findByPhone(userInfo));
        }
        return userList;
    }

    @Override
    public Boolean updateUser(int id, String password, String username, String sex, String phone, String email) {
        String account = userMapper.findAccountByID(id);
        if (!password.equals("")) {
            userMapper.updatePassword(account, password);
        }
        if (!username.equals("")) {
            userMapper.updateUsername(account, username);
        }
        if (sex.equals("男") || sex.equals("1")) {
            userMapper.updateSex(account, 1);
        }
        if (sex.equals("女") || sex.equals("0")) {
            userMapper.updateSex(account, 0);
        }
        if (!phone.equals("")) {
            userMapper.updatePhone(account, phone);
        }
        if (!email.equals("")) {
            userMapper.updateEmail(account, email);
        }
        return true;
    }

    @Override
    public void insertUser(String account, String password, String username, String sex, String phone, String email) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        if (sex.equals("男")) {
            userMapper.insertUser(account, password, username, 1, phone, email, time);
        }
        if (sex.equals("女")) {
            userMapper.insertUser(account, password, username, 0, phone, email, time);
        }
    }

    @Override
    public Boolean updatePhoto(String account, String photo) {
        return userMapper.updatePhoto(account, photo);
    }

    @Override
    public Boolean deleteUser(int id) {
        return userMapper.deleteUser(id);
    }
    @Override
    public String findAccountByPhone(int id) {
        return userMapper.findAccountByPhone(id);
    }

    @Override
    public String findPhotoByID(int id) {
        return userMapper.findPhotoByID(id);
    }
}
